//
//  SetRatePosChannelItemVM.swift
//  kxf
//
//  Created by LNS2 on 2024/6/16.
//

import Foundation
import UIKit

class SetRatePosChannelItemVM: UIView {
    
    let selfHeight = kFitWidth(84)
    
    var isSelect = false
    var tapBlock:(()->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: frame.origin.x, y: frame.origin.y, width: selfHeight, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        self.layer.cornerRadius = kFitWidth(4)
        self.clipsToBounds = true
        self.layer.borderColor = UIColor.clear.cgColor
        self.layer.borderWidth = kFitWidth(1)
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(selfTapAction))
        self.addGestureRecognizer(tap)
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var imgView : UIImageView = {
        let img = UIImageView()
        img.backgroundColor = .clear
        img.isUserInteractionEnabled = true
        img.clipsToBounds = true
        img.contentMode = .scaleAspectFit
        return img
    }()
    lazy var titleLab : UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 14, weight: .medium)
        lab.isUserInteractionEnabled = true
        lab.adjustsFontSizeToFitWidth = true
        lab.textAlignment = .center
        
        return lab
    }()
}

extension SetRatePosChannelItemVM{
    @objc func selfTapAction(){
        if self.isSelect == true{
            return
        }
        self.setSelectStatus(isSelect: true)
        if self.tapBlock != nil{
            self.tapBlock!()
        }
    }
    func setSelectStatus(isSelect:Bool) {
        self.isSelect = isSelect
        if isSelect{
            self.layer.borderColor = UIColor.THEME.cgColor
            self.backgroundColor = WHColorWithAlpha(colorStr: "F10E0E", alpha: 0.04)
        }else{
            self.layer.borderColor = UIColor.clear.cgColor
            self.backgroundColor = .white
        }
    }
}
extension SetRatePosChannelItemVM{
    func initUI() {
        addSubview(imgView)
        addSubview(titleLab)
        
        imgView.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.width.height.equalTo(kFitWidth(46))
            make.top.equalTo(kFitWidth(10))
        }
        titleLab.snp.makeConstraints { make in
            make.left.width.equalToSuperview()
            make.top.equalTo(imgView.snp.bottom).offset(kFitWidth(8))
        }
    }
}
